import mixins from '../mixins';

let { HttpRequest, globalData, cloud, mfutil, URLChangeCommon ,addUserStat } = getApp()
Component({
  mixins: [mixins],
  data: {
    show: true,
    playFunc: null,
    pauseFunc: null,
    endedFunc: null,
    time: 0,
    change: 0,
    flag: false,
    dataNum: {},
    activetype: null,
    accomplishActive: false,
    state: false
  },
  props: {},
  didMount() {
    const { data } = this.props
    this.data.dataNum = this.props.dataNum
    this.data.activetype = this.props.activetype
    this.videoContext = my.createVideoContext(data.id);
    this.setData({
      item: data,
      json: data.json
    })


  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onVideoPlay(func) {
      this.setData({
        playFunc: func
      })
    },
    _hide() {
      this.videoContext.pause();
    },
    play() {
      console.log(123123123123123123);
      
      this.videoContext.play();
    },
    pause() {
      this.videoContext.pause();
    },
    stop() {
      this.videoContext.stop();
    },
    onPlay() {
      this.data.state = true
      if (this.data.state == true) {
        console.log('视频开始播放99999999999999999999999999')
        this.timeChange()
        try {
          this.data.playFunc()
        } catch (e) {

        }
      }

    },
    onPlayError(event) {
      console.log('onPlayError')
      mfutil.launchErrPage(53001)
    },
    timeChange() {
      let { time } = this.data;
      let myTime = setInterval(() => {
        this.setData({
          time: ++time
        })
        console.log(this.data.time)
      }, 1000)
      this.setData({
        myTime
      })
    },
    onPause() {
      let { myTime } = this.data;
      clearInterval(myTime)
      try {
        this.data.pauseFunc()
      } catch (e) {

      }
    },
    onEnded() {
      console.log(this.data.dataNum);

      // 视频播放结束 判断是否做过任务  如果是则不弹出弹窗
      if (this.data.dataNum.done == 0) {
        try {
          this.setData({
            successActive: true
          })
          console.log('视频观看结束')
          this.data.endedFunc()
        } catch (e) {

        }
      } else {
        this.setData({
          successActive: false
        })
      }

    },
    async accomplish() {
      let { item, time, accomplishActive, myTime } = this.data;
      if (time >= item.required_duration && !accomplishActive && this.data.activetype != 'finished') {
        let datetime = new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).replace("T", " ").replace(/\-/g, "/");
        let bodyObj1 = {
          do_datetime: datetime,
          duration: time
        }
        let parmasObj1 = {
          member_id_type: 1,
          member_id: globalData.userInfo.memberId
        }
        let path1 = `/vans-family/api/v2.1/project/vansFamily/cms/forward/post?url=/v1/cms/api/tile/${item.type}/${item.id}/done`;
        let res = await HttpRequest(path1, 'POST', parmasObj1, bodyObj1);
        if (res.data.code == 0) {
          my.showToast({
            type: 'success',
            content: '完成视频观看任务啦！',
            duration: 2000,
          });
          this.setData({
            accomplishActive: true
          })
          clearInterval(myTime)
        } else {
          mfutil.launchErrPage(53001)
        } 
      }
    },
    onTimeUpdate() {
      let { item, time, accomplishActive, flag } = this.data;
      console.log(this.data.dataNum.todo);

      if (!flag && time >= item.required_duration && !accomplishActive && this.data.activetype != 'finished') {
        if (this.data.dataNum.todo >= 1 || this.data.dataNum.done <= 1) {
          this.accomplish()
        }
        this.setData({
          flag: true
        })
      }
    },
    // 修改视频地址
    setSrc(_src) {
      const { item } = this.data;
      this.setData({
        item: { ...item, src: _src }
      })
    },
    urlChange(event) {
      let { item } = event.currentTarget.dataset;
      console.log(item);

      URLChangeCommon(item)
      /**
* 用户行为记录埋点
*/
      let routerArr = getCurrentPages();
      let params = app.globalData.params;
      let channel = app.globalData.channel;
      let UserAccessoptions = {
        grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
        name: routerArr[routerArr.length - 1].data.pagetitle,
        address: this.data.json.tile_info.id,
        String4: item.cta_button_name
      }
      let track = UserAccess(UserAccessoptions)

        // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCTA_'+this.data.json.tile_info.id+'_'+item.cta_button_name
    }

    let tracks = addUserStat(customAccessoptions)
    },
  },
});
